package online.yangcloud.web.service.meta.impl;

import cn.hutool.core.date.DateUtil;
import com.mybatisflex.core.query.QueryWrapper;
import online.yangcloud.common.enumration.YesOrNoEnum;
import online.yangcloud.common.tools.ExceptionTools;
import online.yangcloud.core.mapper.GlobalMapper;
import online.yangcloud.core.model.entity.PreviewConvertEntity;
import online.yangcloud.web.service.meta.PreviewConvertMetaService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author zhuboyang
 * @since 2023年11月02 13:16:09
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PreviewConvertMetaServiceImpl implements PreviewConvertMetaService {

    @Resource
    private GlobalMapper globalMapper;

    @Override
    public void addPreviewTask(PreviewConvertEntity task) {
        ExceptionTools.updateError(globalMapper.acquirePreviewConvert().insertWithPk(task));
    }

    @Override
    public void updatePreviewTask(PreviewConvertEntity task) {
        task.setUpdateTime(DateUtil.date().getTime());
        ExceptionTools.updateError(globalMapper.acquirePreviewConvert().update(task));
    }

    @Override
    public PreviewConvertEntity queryUnExecutedTask() {
        return globalMapper.acquirePreviewConvert().selectOneByQuery(QueryWrapper.create()
                .where(PreviewConvertEntity::getIsDelete).eq(YesOrNoEnum.NO.code())
                .orderBy(PreviewConvertEntity::getCreateTime).asc()
                .limit(0, 1));
    }
}
